//
//  ChartHeaderView.m
//  Mahjong
//
//  Created by 饶首建 on 17/6/5.
//  Copyright © 2017年 com.ikamobile.com. All rights reserved.
//

#import "ChartHeaderView.h"
#import "AAChartView.h"
#import "GameModel.h"
#import "ResultModel.h"
@interface ChartHeaderView()

@property (nonatomic,strong) AAChartView *aaChartView;


@end

@implementation ChartHeaderView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame gameArr:(NSArray *)gameArr matchModel:(MatchModel*)matchModel{
    self = [super initWithFrame:frame];
    if (self) {
        _aaChartView = [[AAChartView alloc]init];
        self.backgroundColor = [UIColor whiteColor];
        _aaChartView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
        [self addSubview:_aaChartView];
        [_aaChartView aa_drawChartWithChartModel:[self chartModel:gameArr matchModel:matchModel]];//图表视图对象调用图表模型对象,绘制最终图形
    }
    return self;
}

- (void)updateChartGameArr:(NSArray *)gameArr matchModel:(MatchModel*)matchModel{
    [_aaChartView aa_refreshChartWithChartModel:[self chartModel:gameArr matchModel:matchModel]];//更新 AAChartModel 数据之后,刷新图表
}

- (AAChartModel*)chartModel:(NSArray *)gameArr matchModel:(MatchModel*)matchModel{
    NSMutableArray *tmp = [NSMutableArray arrayWithArray:gameArr];
    //第几局
    NSMutableArray *setArray = [NSMutableArray new];
    //玩家A的dataSet
    PlayerModel *playerA = matchModel.players.count>0?matchModel.players[0]:nil;
    PlayerModel *playerB = matchModel.players.count>1?matchModel.players[1]:nil;
    PlayerModel *playerC = matchModel.players.count>2?matchModel.players[2]:nil;
    PlayerModel *playerD = matchModel.players.count>3?matchModel.players[3]:nil;
    NSMutableArray *setScoreA = [NSMutableArray new];
    NSMutableArray *setScoreB = [NSMutableArray new];
    NSMutableArray *setScoreC = [NSMutableArray new];
    NSMutableArray *setScoreD = [NSMutableArray new];
    
    [setArray addObject:@"初始状态"];
    [setScoreA addObject:@0];
    [setScoreB addObject:@0];
    [setScoreC addObject:@0];
    [setScoreD addObject:@0];
    //总分
    NSNumber *lastScore;
    for (int i=0; i< tmp.count; i++) {
        [setArray addObject:NSStringFormat(@"第%d局",i+1)];
        GameModel *gm = tmp[i];
        for (ResultModel *rm in gm.resultArr) {
            if ([rm.playerModel.playerId isEqualToString:playerA.playerId]) {
                
                lastScore = [setScoreA lastObject];
                [setScoreA addObject:@(lastScore.integerValue + rm.score)];
            }else if ([rm.playerModel.playerId isEqualToString:playerB.playerId]){
                
                lastScore = [setScoreB lastObject];
                [setScoreB addObject:@(lastScore.integerValue +rm.score)];
            }else if ([rm.playerModel.playerId isEqualToString:playerC.playerId]){
                
                lastScore = [setScoreC lastObject];
                [setScoreC addObject:@(lastScore.integerValue +rm.score)];
            }else if ([rm.playerModel.playerId isEqualToString:playerD.playerId]){
                
                lastScore = [setScoreD lastObject];
                [setScoreD addObject:@(lastScore.integerValue +rm.score)];
            }
        }
    }
    //seriesSet
    NSMutableArray *seriesSet = [NSMutableArray new];
    if (matchModel.players.count == 1) {
        [seriesSet removeAllObjects];
        [seriesSet addObject:AAObject(AASeriesElement).nameSet(playerA.playerName?:@"玩家A").dataSet(setScoreA)];
    }else if (matchModel.players.count == 2) {
        [seriesSet removeAllObjects];
        [seriesSet addObject:AAObject(AASeriesElement).nameSet(playerA.playerName?:@"玩家A").dataSet(setScoreA)];
        [seriesSet addObject:AAObject(AASeriesElement).nameSet(playerB.playerName?:@"玩家B").dataSet(setScoreB)];
    }else if (matchModel.players.count == 3) {
        [seriesSet removeAllObjects];
        [seriesSet addObject:AAObject(AASeriesElement).nameSet(playerA.playerName?:@"玩家A").dataSet(setScoreA)];
        [seriesSet addObject:AAObject(AASeriesElement).nameSet(playerB.playerName?:@"玩家B").dataSet(setScoreB)];
        [seriesSet addObject:AAObject(AASeriesElement).nameSet(playerC.playerName?:@"玩家C").dataSet(setScoreC)];
    }else if (matchModel.players.count >= 4) {
        [seriesSet removeAllObjects];
        [seriesSet addObject:AAObject(AASeriesElement).nameSet(playerA.playerName?:@"玩家A").dataSet(setScoreA)];
        [seriesSet addObject:AAObject(AASeriesElement).nameSet(playerB.playerName?:@"玩家B").dataSet(setScoreB)];
        [seriesSet addObject:AAObject(AASeriesElement).nameSet(playerC.playerName?:@"玩家C").dataSet(setScoreC)];
        [seriesSet addObject:AAObject(AASeriesElement).nameSet(playerD.playerName?:@"玩家D").dataSet(setScoreD)];
    }
    
    AAChartModel *chartModel= AAObject(AAChartModel)
    .chartTypeSet(AAChartTypeSpline)//设置图表的类型(这里以设置的为柱状图为例)
    .titleSet(@"牌局数据")//设置图表标题
    .subtitleSet(@"")//设置图表副标题
    .categoriesSet(setArray)//设置图表横轴的内容
    .yAxisTitleSet(@"分数")//设置图表 y 轴的单位
    .seriesSet(seriesSet);
    chartModel.markerRadius = @0.f;//隐藏点
    chartModel.dataLabelEnabled = YES;//显示数值
    return chartModel;
}


@end
